/*
 * Created on 2004.25.11
 *
 */
package lv.solcraft.replication.client.jmx;

import java.io.IOException;
import java.io.InputStream;

import lv.solcraft.replication.common.util.SpringUtils;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.springframework.context.support.GenericApplicationContext;

/**
 * @author Ilgvars
 */
public class ClientDeployment implements ClientDeploymentMBean {

    private static Logger log = Logger.getLogger(ClientDeployment.class);

    private String dataSource;

    private String serverHost;

    private String serverPort;

    private String sessionCronExpression;

    private String serverName;

    private long lastSessionStarted;

    private long lastSessionFinished;

    private GenericApplicationContext appContext;

    public String getDataSource() {
        return dataSource;
    }

    public void setDataSource(String dataSource) {
        this.dataSource = dataSource;
    }

    public String getServerHost() {
        return serverHost;
    }

    public void setServerHost(String serverHost) {
        this.serverHost = serverHost;
    }

    public String getServerPort() {
        return serverPort;
    }

    public void setServerPort(String serverPort) {
        this.serverPort = serverPort;
    }

    public String getSessionCronExpression() {
        return sessionCronExpression;
    }

    public void setSessionCronExpression(String sessionCronExpression) {
        this.sessionCronExpression = sessionCronExpression;
    }

    private GenericApplicationContext createAppContext() {
        try {
            InputStream is = this.getClass().getClassLoader().getResourceAsStream("client-context-template.xml");
            String template = IOUtils.toString(is);
            String contextFile = createContextFile(template);

            log.info("Generated bean factory for client " + getClientName() + "\n" + contextFile);

            return SpringUtils.createAppContext(contextFile);
        } catch (IOException e) {
            throw new RuntimeException("Error while creating Spring bean factory. ", e);
        }
    }

    private String createContextFile(String template) {
        String result = template;

        result = result.replaceAll("#session.cron#", sessionCronExpression.trim());
        result = result.replaceAll("#server.host#", serverHost.trim());
        result = result.replaceAll("#server.port#", serverPort.trim());
        result = result.replaceAll("#datasource#", dataSource.trim());
        result = result.replaceAll("#client#", getClientName());
        result = result.replaceAll("#server.name#", serverName);
        return result;

    }

    private String getClientName() {
        // TODO fix
        throw new UnsupportedOperationException("not implemented");
    }

    public String getServerName() {
        return serverName;
    }

    public void setServerName(String serverName) {
        this.serverName = serverName;
    }

    public ClientDeployment getSelf() {
        return this;
    }

    public long getLastSessionFinished() {
        return lastSessionFinished;
    }

    public void setLastSessionFinished(long lastSessionFinished) {
        this.lastSessionFinished = lastSessionFinished;
    }

    public long getLastSessionStarted() {
        return lastSessionStarted;
    }

    public void setLastSessionStarted(long lastSessionStarted) {
        this.lastSessionStarted = lastSessionStarted;
    }

}
